package baptizeattribute.event;

import baptizeattribute.baptize.Baptize;
import baptizeattribute.data.Data;
import baptizeattribute.inventory.Inv;
import baptizeattribute.utils.Tools;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class InventoryClose implements Listener {
    @EventHandler
    public void event(InventoryCloseEvent e){
        Inventory ivv = e.getInventory();
        if (ivv.getTitle().equals(Inv.ivName)){
            Player p = (Player) e.getPlayer();
//            if (iv.getItem(Inv.itemOneID)!=null || iv.getItem(Inv.itemTwoID)!=null){
//                p.sendMessage(Data.config.getInfo()+"§c 请取回物品再关闭背包以免丢失");
//                new BukkitRunnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(100);
//                        } catch (InterruptedException e1) {
//                            e1.printStackTrace();
//                        }
//                        p.openInventory(Inv.playerIv.get(p.getUniqueId()));
//                    }
//                }.runTaskAsynchronously(Baptize.getMain());
//            }else {
//                Data.guiFlag.put(p.getUniqueId(),false);
//            }
            if (Data.guiFlag.get(p.getUniqueId())!=null && Data.guiFlag.get(p.getUniqueId())){
                Inventory iv = Inv.playerIv.get(p.getUniqueId());
                if (iv.getItem(Inv.itemOneID)!=null){
                    ItemStack item = iv.getItem(Inv.itemOneID);
                    Tools.addPlayerByItem(p,item,item.getAmount());
                }
                if (iv.getItem(Inv.itemTwoID)!=null){
                    ItemStack item = iv.getItem(Inv.itemTwoID);
                    Tools.addPlayerByItem(p,item,item.getAmount());
                }
            }
        }
    }
}
